<!-- main content -->
<div class="container">
    <div class="row-fluid">
        <div class="span12">
            <div class="w-box">
                <div class="w-box-header">
                    <h4>Создание нового профиля</h4>
                </div>
                <div class="w-box-content">
                    <form id="create_user" action="ajax/createUser.php" method="post">
                        <div class="formSep">
                            <label for="u_username">Логин</label>
                            <input type="text" id="u_username" name="u_username" class="span3" value=""/>
                        </div>
                        <div class="formSep">
                            <label for="u_name">Имя пользователя</label>
                            <input type="text" id="u_name" name="u_name" class="span3" value=""/>
                        </div>
                        <input type="hidden" id="u_group" name="u_group" value="2"/>

                        <div class="span4">
                            <label for="sb_call">Субагент / Субагент</label>
                            <input type="checkbox" id="sb_call"/>
                        </div>
                        <div class="formSep" id="parent_hide">
                            <label for="u_parent">Список агентов</label>
                            <select id="u_parent" name="u_parent" class="span3">
                                <option value="">Выберите</option>
                                <option value="1" selected>Смартсим</option>
                            </select>
                        </div>
                        <div class="formSep">
                            <div class="span4">
                                <div class="span6">
                                    <input type="password" name="u_password" id="u_password" class="span12">

                                    <div id="pwdMeter" class="progress progress-danger">
                                        <div class="bar" style="width: 0%"></div>
                                        <span class="pwdText"></span>
                                    </div>
                                </div>
                                <div class="span6">
                                    <div id="pwdGenerator"></div>
                                </div>
                            </div>
                        </div>
                        <div class="formSep">
                            <label for="u_email">Email</label>
                            <input type="text" id="u_email" name="u_email" class="span3" value=""/>
                        </div>
                        <div class="formSep sepH_b">
                            <button class="btn btn-beoro-3" type="submit">Сохранить</button>
                            <a href="#" class="btn btn-link">Отменить</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="footer_space"></div>
</div>
<!-- jQuery form -->
<script type="text/javascript">
    function agent() {
        $('#parent_hide').hide();
        $("#u_group").val(2);
        $('option', $("#u_parent")).remove();
        $("#u_parent").prepend('<option value="">Выберите</option><option value="1" selected>Смартсим</option>');
    }
    function subagent() {
        $('#parent_hide').show();
        $("#u_group").val(3);
        $.ajax({
            url: "ajax/createUser.php",
            dataType: "html",
            type: "POST",
            data: {get_agent: 'Y'},
            success: function (data) {
                $('option', $("#u_parent")).remove();
                $("#u_parent").prepend('<option value="">Выберите</option>' + data);
            },
            error: function (data) {
                $.sticky('Список не загружен', {autoclose: 3000, position: "top-right", type: "st-error"});
            }
        });
    }
    $(document).ready(function () {
        $('#parent_hide').hide();
        $("#sb_call").iButton({
            labelOn: "Субагент",
            labelOff: "Агент",
            change: function ($input) {
                $input.is(":checked") ? subagent() : agent();
            }
        });
        $('#u_password').pwdMeter({
            minLength: 6,
            displayGeneratePassword: true,
            generatePassText: 'Сгенерировать пароль',
            randomPassLength: 16,
            neutralText: "",
            veryWeakText: "Очень слабый",
            weakText: "Слабый",
            mediumText: "Средний",
            strongText: "Сложный",
            veryStrongText: "Очень сложный"
        });
        // bind form using ajaxForm
        //$("#u_password, #u_repassword").focus(function () {
        //    $("#u_password, #u_repassword").css('color', 'black');
        //})
        $('#create_user').ajaxForm({
            beforeSubmit: validate,
            // dataType identifies the expected content type of the server response
            dataType: 'json',
            // success identifies the function to invoke when the server response
            // has been received
            success: processJson
        });
    });
    function processJson(data) {
        // 'data' is the json object returned from the server
        //$('#create_user')[0].reset();
        //console.log(data);
        $.sticky(data.message, {autoclose: 6000, position: "top-center", type: data.type});

        //$(window).attr("location",".");

    }
    function validate(formData, jqForm, options) {
        // formData is an array of objects representing the name and value of each field
        // that will be sent to the server;  it takes the following form:
        //
        // [
        //     { name:  username, value: valueOfUsernameInput },
        //     { name:  password, value: valueOfPasswordInput }
        // ]
        //
        // To validate, we can examine the contents of this array to see if the
        // username and password fields have values.  If either value evaluates
        // to false then we return false from this method.


        for (var i = 0; i < formData.length; i++) {
            if (!formData[i].value) {
                bootbox.alert("Заполните все поля", function () {
                    //console.log("Alert Callback");
                });
                return false;
            }
        }
        //processJson(data);
        //if ($("#u_password").val() != $("#u_repassword").val()) {
        //    $("#u_password, #u_repassword").css('color', 'red');
        //    $.sticky('Пароли не совпадают', {autoclose: 3000, position: "top-right", type: "st-error"});
        //    return false;
        //}

    }
</script>